﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Samples;

namespace FlashlightWPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += (s, e) =>
                {
                    FullScreenHelper.GoFullscreen(this);
                };

            // Check Mouse
            this.MouseMove += (s, e) =>
                {
                    UpdateFlashlight(e.GetPosition(this));
                };

            // Create Screen Shot
            Bitmap bmp = CaptureScreen();
            BitmapSource bmpSrc = CreateBitmapSource(bmp);
            backgroundImage.Source = bmpSrc;
        }

        private void UpdateFlashlight(System.Windows.Point point)
        {
            EllipseGeometry eg = new EllipseGeometry(point, 75, 75);
            backgroundImage.Clip = eg;
        }

        public Bitmap CaptureScreen()
        {
            Bitmap bmp = new Bitmap(SystemInformation.VirtualScreen.Width, 
                SystemInformation.VirtualScreen.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
            g.Dispose();
            return bmp;
        }

        public static BitmapSource CreateBitmapSource(System.Drawing.Bitmap bmp)
        {
            return Imaging.CreateBitmapSourceFromHBitmap(
                bmp.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
    }
}
